﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DbExecuter.DataLayer.commands
{
    public class TSelectCommand : TCommandBase
    {
        #region ctors
        public TSelectCommand(SqlConnection connection)
            : base(connection)
        {
        }
        #endregion

        #region private methods
        private void SetCommandText(TSelectClause selectClause, TFromClause fromClause, TWhereClause whereClause)
        {
            _CommandText = String.Format("\nUSE {0}\n{1} {2} {3}"
                , fromClause.FromTable.BaseCatalog.DatabaseName
                , selectClause.ToString()
                , fromClause.ToString()
                , (whereClause != null)
                ? whereClause.ToString() : "");
        }
        #endregion

        #region public methods
        public DataTable SelectWhere(TSelectClause selectClause, TFromClause fromClause, TWhereClause whereClause)
        {
            SetCommandText(selectClause, fromClause, whereClause);

            CommandParameters = new List<TCommandParameter>(whereClause.WhereParameters);
            
            ConstructCommand(CommandText);

            SqlDataAdapter da = new SqlDataAdapter(Command);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
        public DataTable SelectAll(TSelectClause selectClause, TFromClause fromClause)
        {
            SetCommandText(selectClause, fromClause, null);

            ConstructCommand(CommandText);

            SqlDataAdapter da = new SqlDataAdapter(Command);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
        public SqlDataReader SelectWhereReader(TSelectClause selectClause, TFromClause fromClause, TWhereClause whereClause)
        {
            SetCommandText(selectClause, fromClause, whereClause);

            CommandParameters = new List<TCommandParameter>(whereClause.WhereParameters);

            ConstructCommand(CommandText);
            SqlConnection tempConnection = new SqlConnection(CommandText);
            Command.Connection = tempConnection;
            tempConnection.Open();
            SqlDataReader rdr = Command.ExecuteReader(CommandBehavior.CloseConnection);
            return rdr;
        }
        public SqlDataReader SelectAllReader(TSelectClause selectClause, TFromClause fromClause)
        {
            SetCommandText(selectClause, fromClause, null);

            ConstructCommand(CommandText);

            SqlConnection tempConnection = new SqlConnection(CommandText);
            Command.Connection = tempConnection;
            tempConnection.Open();
            SqlDataReader rdr = Command.ExecuteReader(CommandBehavior.CloseConnection);
            return rdr;
        }
        #endregion

        #region overriden methods
        public override string ToString()
        {
            return _CommandText;
        } 
        #endregion

        #region properties
        private string _CommandText;
        public string CommandText
        {
            get { return _CommandText; }
        }
        #endregion
    }
}
